package com.chinairi.mall.controller;


import com.chinairi.common.utils.ResponseUtil;
import com.chinairi.mall.controller.viewobject.ApproveDbIdVO;
import com.chinairi.mall.controller.viewobject.GoodsInfoVO;
import com.chinairi.mall.controller.viewobject.GoodsInforExVO;
import com.chinairi.mall.model.*;
import com.chinairi.mall.service.IGoodsInfoService;
import com.chinairi.mall.service.IMaterialService;
import com.chinairi.mall.service.IOrderInfoService;
import com.chinairi.mall.service.IUserCompanyInfoService;
import com.chinairi.mall.service.modelobject.GoodsAllInfoByMatDbIdMO;
import com.chinairi.mall.service.modelobject.HomeGoodsMO;
import com.chinairi.mall.service.modelobject.ViewContractMatRelMO;
import com.chinairi.mall.util.ExcelUploadDataListenerUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.chinairi.common.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 供应商查看商品的一些基本信息和一些维护功能，比如上下架，起订量的设置
 * 前端控制器
 * </p>
 *
 * @author Webgasd
 * @since 2020-09-12
 */
@RestController
@RequestMapping("/goods-info")
public class GoodsInfoController extends BaseController {

    @Autowired
    private IGoodsInfoService iGoodsInfoService;
    @Autowired
    private IMaterialService iMaterialService;
    @Autowired
    private IUserCompanyInfoService iUserCompanyInfoService;
    @Autowired
    private IOrderInfoService iOrderInfoService;

    /**
     * 获取供应商的合同
     *
     * @param
     * @throws IOException
     */
    @GetMapping("/getContractList")
    public void getContractList(String comaContractFlag) throws Exception {
        List<ViewContractMatRelMO> contractMatInforList = iGoodsInfoService.getContractList(comaContractFlag);
        ResponseUtil.showMessage("success", "获取成功", contractMatInforList);
    }

    /**
     * 根据合同主键获取物料
     *
     * @param contDbid
     * @throws IOException
     */
    @GetMapping("/getGoodsInfo")
    public void getGoodsInfo(String contDbid) throws IOException {
        List<GoodsInfoVO> goodsInfoList = iGoodsInfoService.getByContDbid(contDbid);
        ResponseUtil.showMessage("success", "获取成功", goodsInfoList);
    }

    /**
     * 获取物料特征值
     *
     * @param matDbid
     * @throws IOException
     */
    @GetMapping("/getMatFeatureValue")
    public void getMatFeatureValue(String matDbid) throws IOException {
        List<MatFeatureValue> matFeatureValueList = iMaterialService.getMatFeatureValue(matDbid);
        ResponseUtil.showMessage("success", "获取成功", matFeatureValueList);
    }

    /**
     * 商品上架申请
     *
     * @param goodsInfoVO
     */
    @PostMapping("/add")
    public void add(@RequestBody GoodsInfoVO goodsInfoVO) throws IOException {
        iGoodsInfoService.add(goodsInfoVO);
        ResponseUtil.showMessage("success", "新增成功", "");
    }

    @GetMapping("/getModel")
    public void getModel(HttpServletResponse response) throws IOException {
       iGoodsInfoService.getModel(response);
    }

    /**
     * 文件上传
     * <p>
     * 1. 创建excel对应的实体对象 参照{@link GoodsInfo}
     * <p>
     * 2. 由于默认一行行的读取excel，所以需要创建excel一行一行的回调监听器，参照{@link ExcelUploadDataListenerUtil}
     * <p>
     * 3. 直接读即可
     */
    @PostMapping("upload")
    @ResponseBody
    public void uploadGoods(MultipartFile file) throws IOException {
        iGoodsInfoService.uploadFile(file);
        ResponseUtil.showMessage("success","上传成功","");
    }

    /**
     * 产品信息维护
     *
     * @param goodsInfoVO
     * @throws IOException
     */
    @PutMapping("/update")
    public void update(@RequestBody GoodsInfoVO goodsInfoVO) throws IOException {
        iGoodsInfoService.updateGoods(goodsInfoVO);
        ResponseUtil.showMessage("success", "更新成功", "");
    }

    /**
     * 单个设置最小起订量和库存
     * 上下架状态
     *
     * @param goodsInfoVO
     * @throws IOException
     */
    @PutMapping("/updateGoods")
    public void updateMin(@RequestBody GoodsInfoVO goodsInfoVO) throws IOException {
        goodsInfoVO.setGoodsShelfState(0);
        goodsInfoVO.setGoodsApproveStatus(0);
        iGoodsInfoService.updateById(goodsInfoVO);
        ResponseUtil.showMessage("success", "更新成功", "");
    }

    /**
     * 批量设置更新
     * 最小起订量和库存量
     *
     * @param goodsInfoList
     * @throws IOException
     */
    @PutMapping("/updateMul")
    public void updateMul(@RequestBody List<GoodsInfo> goodsInfoList) throws IOException {
        for (GoodsInfo goodsInfo : goodsInfoList) {
            goodsInfo.setGoodsShelfState(0);
            goodsInfo.setGoodsApproveStatus(0);
        }
        iGoodsInfoService.updateBatchById(goodsInfoList);
        ResponseUtil.showMessage("success", "批量更新成功", "");
    }

    /**
     * 获取商品列表
     *
     * @param goodsInfoVO 搜索体
     */
    @GetMapping("/getList")
    public void getList(GoodsInfoVO goodsInfoVO, HttpServletRequest request) throws IOException {
        String csDbId = iGoodsInfoService.getCsDbid(goodsInfoVO.getType(), request);
        List<GoodsInfo> list = iGoodsInfoService.getListByCsDbId(goodsInfoVO, csDbId);
        List<GoodsInfoVO> listVO=new ArrayList<>();
        for (GoodsInfo goodsInfo:list) {
            GoodsInfoVO goodsInfoVO1=new GoodsInfoVO();
            BeanUtils.copyProperties(goodsInfo,goodsInfoVO1);
            goodsInfoVO1.setMatCode(iOrderInfoService.getMatCodeByGoodsId(goodsInfoVO1.getGoodsDbid()));
            listVO.add(goodsInfoVO1);
        }
        GoodsInforExVO goodsInforExVO = new GoodsInforExVO();
        goodsInforExVO.setCsDbid(csDbId);
        goodsInforExVO.setGoodsInfoList(listVO);
        ResponseUtil.showMessage("success", "获取成功", goodsInforExVO);
    }

    /**
     * 根据商品id获取商品信息
     *
     * @param goodsDbid 主键
     * @throws IOException 异常
     */
    @GetMapping("/getByDbid")
    public void getByDbid(String goodsDbid) throws IOException {
        GoodsInfoVO goodsInfoVO = iGoodsInfoService.getByDbid(goodsDbid);
        ResponseUtil.showMessage("success", "获取成功", goodsInfoVO);
    }

    /**
     * 删除某个商品
     *
     * @param goodsDbid
     * @throws IOException
     */
    @DeleteMapping("/delete")
    public void delete(String goodsDbid) throws IOException {
        iGoodsInfoService.deleteById(goodsDbid);
        ResponseUtil.showMessage("success", "删除完成", "");
    }

    /**
     * 根据物料的dbId获取商品和供应商所有的信息
     *
     * @param dbIdList 物料dbIdList
     * @return List
     */
    @GetMapping("/selectAllInfoByMatDbId")
    public void selectAllInfoByMatDbId(@RequestParam(value = "dbIdList") List<String> dbIdList) throws IOException {
        Map<String, List<GoodsAllInfoByMatDbIdMO>> resultMap = iGoodsInfoService.selectAllInfoByMatDbId(dbIdList);
        ResponseUtil.showMessage("success", "获取成功", resultMap);
    }

    /**
     * 批量提交实物商品审核
     *
     * @param approveDbIdVoList
     * @throws IOException
     */
    @PostMapping("/updateStatus")
    public void updateStatus(@RequestBody List<ApproveDbIdVO> approveDbIdVoList) throws IOException {
        iGoodsInfoService.updateStatus(approveDbIdVoList);
        ResponseUtil.showMessage("success", "添加审核成功", null);
    }

    /**
     * 获取首页商品列表
     * 搜索条件为：
     * goodsName  商品名称     全模糊搜索
     * matCode    物料类别编码  左起始搜索  主要是侧边栏进入后使用
     * csShopName 店铺名称     全模糊搜索
     *
     * @param homeGoodsMo
     * @throws IOException
     */
    @GetMapping("/getHomeGoods")
    public void getHomeGoods(HomeGoodsMO homeGoodsMo) throws IOException {
        List<HomeGoodsMO> homeGoodsMOList = iGoodsInfoService.getHomeGoods(homeGoodsMo);
        ResponseUtil.showMessage("success", "获取成功", homeGoodsMOList);
    }

    /**
     * 获取热销5个商品
     *
     * @throws IOException
     */
    @GetMapping("/getHotSaleGoods")
    public void getHotSaleGoods() throws IOException {
        List<HomeGoodsMO> hotSaleGoodList = iGoodsInfoService.getHotSaleGoods();
        ResponseUtil.showMessage("success", "获取成功", hotSaleGoodList);
    }

    /**
     * 获取首页5个大宗原料
     * @throws IOException
     */
    @GetMapping("/getLargeGoods")
    public void getLargeGoods() throws IOException {
        List<HomeGoodsMO> hotSaleGoodList = iGoodsInfoService.getHotSaleGoods();
        ResponseUtil.showMessage("success", "获取成功", hotSaleGoodList);
    }
    /**
     * 获取首页5个办公商品
     * @throws IOException
     */
    @GetMapping("/getOfficeGoods")
    public void getOfficeGoods() throws IOException {
        List<HomeGoodsMO> hotSaleGoodList = iGoodsInfoService.getHotSaleGoods();
        ResponseUtil.showMessage("success", "获取成功", hotSaleGoodList);
    }



}
